TMDBService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 97
dl 0
loc 222
c 0
b 0
f 0
rs 10

5 Functions

Rating   Name   Duplication   Size   Complexity  
A buildBaseURL 0 11 2
A buildEndpointsConfiguration 0 12 1
A updateClassConfiguration 0 9 1
A includeAdult 0 10 1
A bootEndpoints 0 22 1
1
import { EndpointConfiguration } from '../interfaces/common/EndpointConfiguration';
2
import {
3
    Certification as CertificationEndpoint,
4
    Changes as ChangesEndpoint,
5
    Company as CompanyEndpoint,
6
    Configuration as ConfigurationEndpoint,
7
    Credit as CreditEndpoint,
8
    Discover as DiscoverEndpoint,
9
    Genre as GenreEndpoint,
10
    Movie as MovieEndpoint,
11
    Network as NetworkEndpoint,
12
    Trending as TrendingEndpoint,
13
    Person as PersonEndpoint,
14
    Review as ReviewEndpoint,
15
    Search as SearchEndpoint,
16
    TV as TVEndpoint,
17
    TVSeason as TVSeasonEndpoint,
18
    TVEpisode as TVEpisodeEndpoint,
19
} from '../endpoints';
20
21
export class TMDBService {
22
    /**
23
     * Whether or not we want to use `https`
24
     * @var boolean
25
     */
26
    readonly secure: boolean = true;
27
28
    /**
29
     * The main API url
30
     * @var string
31
     */
32
    private host: string = 'api.themoviedb.org';
33
34
    /**
35
     * API Version we are going to use
36
     * @var number
37
     */
38
    private version: number = 3;
39
40
    /**
41
     * API Key we are going to use
42
     * @var string
43
     */
44
    readonly key: string;
45
46
    /**
47
     * Whether we want to include adult media in the response
48
     * @var boolean
49
     */
50
    private adult: boolean = false;
51
52
    /**
53
     * Base API URL
54
     * @var string|null
55
     */
56
    private baseUrl: string | null;
57
58
    /**
59
     * Predefined Endpoint Configuration Object
60
     * @var EndpointConfiguration
61
     */
62
    private endpointConfiguration: EndpointConfiguration;
63
64
    /**
65
     * certification Endpoint Instance
66
     * @var CertificationEndpoint
67
     */
68
    public certification: CertificationEndpoint;
69
70
    /**
71
     * Changes Endpoint Instance
72
     * @var ChangesEndpoint
73
     */
74
    public changes: ChangesEndpoint;
75
76
    /**
77
     * Companies Endpoint Instance
78
     * @var CompanyEndpoint
79
     */
80
    public company: CompanyEndpoint;
81
82
    /**
83
     * Configuration Endpoint Instance
84
     * @var ConfigurationEndpoint
85
     */
86
    public configuration: ConfigurationEndpoint;
87
88
    /**
89
     * Credit Endpoint Instance
90
     * @var CreditEndpoint
91
     */
92
    public credit: CreditEndpoint;
93
94
    /**
95
     * Discover Endpoint Instance
96
     * @var DiscoverEndpoint
97
     */
98
    public discover: DiscoverEndpoint;
99
100
    /**
101
     * Genre Endpoint Instance
102
     * @var GenreEndpoint
103
     */
104
    public genre: GenreEndpoint;
105
106
    /**
107
     * Movie Endpoint Instance
108
     * @var MovieEndpoint
109
     */
110
    public movie: MovieEndpoint;
111
112
    /**
113
     * Network Endpoint Instance
114
     * @var NetworkEndpoint
115
     */
116
    public network: NetworkEndpoint;
117
118
    /**
119
     * Trending Endpoint Instance
120
     * @var TrendingEndpoint
121
     */
122
    public trending: TrendingEndpoint;
123
124
    /**
125
     * Person Endpoint Instance
126
     * @var PersonEndpoint
127
     */
128
    public person: PersonEndpoint;
129
130
    /**
131
     * Search Endpoint Instance
132
     * @var SearchEndpoint
133
     */
134
    public search: SearchEndpoint;
135
136
    /**
137
     * Review Endpoint Instance
138
     * @var ReviewEndpoint
139
     */
140
    public review: ReviewEndpoint;
141
142
    /**
143
     * TV Endpoint Instance
144
     * @var TVEndpoint
145
     */
146
    public tv: TVEndpoint;
147
148
    /**
149
     * TV Season Endpoint Instance
150
     * @var TVSeasonEndpoint
151
     */
152
    public tvSeason: TVSeasonEndpoint;
153
154
    /**
155
     * TV Episode Endpoint Instance
156
     * @var TVEpisodeEndpoint
157
     */
158
    public tvEpisode: TVEpisodeEndpoint;
159
160
    /**
161
     * Class Constructor
162
     * @param apiKey
163
     * @param secure
164
     * @param version
165
     * @param host
166
     */
167
    public constructor(apiKey: string, secure: boolean = true, version: number = 3, host: string = 'api.themoviedb.org') {
168
        this.key = apiKey;
169
        this.secure = secure;
170
        this.version = version;
171
        this.host = host;
172
        this.buildBaseURL();
173
    }
174
175
    /**
176
     * By default, everything marked with adult will be excluded.
177
     * This options allows to include adult content in the response.
178
     * @return TMDBService
179
     */
180
    public includeAdult(): TMDBService {
181
        this.adult = true;
182
        this.updateClassConfiguration();
183
        return this;
184
    }
185
186
    /**
187
     * Build Base API URL
188
     * @return TMDBService
189
     */
190
    private buildBaseURL(): TMDBService {
191
        const protocol = this.secure ? 'https:' : 'http:';
192
        const { host, version } = this;
193
        this.baseUrl = `${protocol}//${host}/${version}`;
194
        this.updateClassConfiguration();
195
        return this;
196
    }
197
198
    /**
199
     * Load all endpoints as class properties
200
     */
201
    private bootEndpoints(): TMDBService {
202
        this.certification  = new CertificationEndpoint(this.endpointConfiguration);
203
        this.changes = new ChangesEndpoint(this.endpointConfiguration);
204
        this.company = new CompanyEndpoint(this.endpointConfiguration);
205
        this.configuration = new ConfigurationEndpoint(this.endpointConfiguration);
206
        this.credit = new CreditEndpoint(this.endpointConfiguration);
207
        this.discover = new DiscoverEndpoint(this.endpointConfiguration);
208
        this.genre = new GenreEndpoint(this.endpointConfiguration);
209
        this.movie = new MovieEndpoint(this.endpointConfiguration);
210
        this.network = new NetworkEndpoint(this.endpointConfiguration);
211
        this.trending = new TrendingEndpoint(this.endpointConfiguration);
212
        this.person = new PersonEndpoint(this.endpointConfiguration);
213
        this.review = new ReviewEndpoint(this.endpointConfiguration);
214
        this.search = new SearchEndpoint(this.endpointConfiguration);
215
        this.tv = new TVEndpoint(this.endpointConfiguration);
216
        this.tvSeason = new TVSeasonEndpoint(this.endpointConfiguration);
217
        this.tvEpisode = new TVEpisodeEndpoint(this.endpointConfiguration);
218
        return this;
219
    }
220
221
    /**
222
     * Build endpoints configuration
223
     * @return TMDBService
224
     */
225
    private buildEndpointsConfiguration(): TMDBService {
226
        this.endpointConfiguration = {
227
            host: this.baseUrl,
228
            key: this.key,
229
            adult: this.adult,
230
        };
231
        return this;
232
    }
233
234
    /**
235
     * Update Class Configuration
236
     * @return TMDBService
237
     */
238
    private updateClassConfiguration(): TMDBService {
239
        this.buildEndpointsConfiguration();
240
        this.bootEndpoints();
241
        return this;
242
    }
243
244
}
245